<?php
namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

/**
 * 代理用户
 */
class UserAgent extends Base
{
    protected $table = 'user_agent';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /**
     * 新增代理用户
     */
    public function agentUserAdd($userId, $rate=0)
    {
        if(empty($userId)) {
            return false;
        }
        $log = [];
        $log['user_id'] = $userId;
        $log['rate'] = (int) $rate;
        $log['status'] = 1;
        $result = DB::table($this->table)->insertGetId($log);

        return $result;
    }

    /**
     * 编辑代理用户
     */
    public function agentUserEdit($userId, $rate=0)
    {
        if(empty($userId)) {
            return false;
        }

        $res = DB::table($this->table)->where('user_id', $userId)->where('status', 1)->update(['rate' => $rate]);

        return $res;
    }

    /**
     * 设置代理用户密码
     */
    public function agentUserSetPassword($userId, $password)
    {
        if(empty($userId) || empty($password)) {
            return false;
        }

        $res = DB::table($this->table)->where('user_id', $userId)->where('status', 1)->update(['password' => $password]);

        return $res;
    }

    /**
     * 删除代理用户
     */
    public function agentUserDel($userId)
    {
        if(empty($userId)) {
            return false;
        }

        $res = DB::table($this->table)->where('user_id', $userId)->where('status', 1)->update(['status' => 0]);

        return $res;
    }

    /**
     * 删除批量代理用户
     */
    public function agentUserDelList($userIds)
    {
        if(empty($userIds)) {
            return false;
        }

        $res = DB::table($this->table)->whereIn('user_id', $userIds)->where('status', 1)->update(['status' => 0]);

        return $res;
    }

    /**
     * 获取代理用户
     */
    public function getUserAgent($userId){
        $info = DB::table($this->table)
            ->where("user_id", $userId)
            ->where("status", 1)
            ->first();
        $info = self::objToArray($info);

        return $info;
    }



}
